package com.ymatou.apigateway.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqProductOnBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqProductOnCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 商品上架Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqProductOn {
	private static SqProductOnBean sqproductonBean;
	private static SqProductOnCall sqproductonCall;
	private static OpenapiReq openapiReq;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	// vip渠道
	private static int rootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品上架Test");
		
	}

	@Before
	public void caseUp() {
		sqproductonBean = new SqProductOnBean();
		sqproductonCall = new SqProductOnCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.product.onsale");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqproductonCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqProductOn_001() {
		Logger.start(true, "正确数据，成功");
		try {
			//新建两个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					"", false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductBean addAgentProductBean1 = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					"", false);
			String productId1 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean1);
			//下架
			AddAgentProductCallService.SetOffshelf(productId, rootUserId);
			AddAgentProductCallService.SetOffshelf(productId1, rootUserId);
			//批量上架
			sqproductonBean.setUserId(rootUserId);
			sqproductonBean.setProductId(Arrays.asList(productId,productId1));
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) rootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductonBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.onsale",
							app_id);
			sqproductonCall.setUrl(url);
			//openapi设置完成
			
			sqproductonCall.setData(openapiReq);
			sqproductonCall.callService();
			Logger.verifyEquals("0000", sqproductonCall.getString("code"), "验证code");
			//验证商品状态为下架状态
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			Logger.verifyEquals(1,agentProductPo.getStatus(),"验证上架状态");
			
			AgentProductPo agentProductPo1 = ymtProductsIWapper.selectAgentProductByProductId(productId1);
			Logger.verifyEquals(1,agentProductPo1.getStatus(),"验证上架状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}